package xarticle

import (
	"time"

	"gitee.com/xiaoyutab/xgotool/optional/xnsq"
	"gitee.com/xiaoyutab/xgotool/xerror"
)

// 审核文章信息
//
//	edit_id	编辑文章的ID
//	audit	审核状态
//	msg		审核备注
//	uid		审核人ID
func Audit(edit_id uint, audit uint8, msg string, uid uint) error {
	if _default.DB == nil {
		return xerror.New("数据库未连接")
	}
	if audit == 0 {
		return xerror.New("不能设置为待审核状态")
	}
	// 获取审核条目信息
	mod_edits, err := EditInfo(edit_id, true)
	if err != nil {
		return err
	}
	mod_edits.AuditStatus = audit
	mod_edits.AuditUserId = uid
	mod_edits.AuditError = msg
	mod_edits.UpdatedAt = time.Now().Format(time.DateTime)
	err = _default.DB.Table(_default.ArticleEditsName).Save(mod_edits).Error
	if err != nil {
		return err
	}
	// 如果是审核通过，需要同步到主文章表中
	if audit == 99 {
		xnsq.Set("xarticle.audit99", mod_edits)
	}
	return nil
}
